using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

namespace Pyramid
{
    class ClippedLaplacianBlending:Blend
    {
        
       
      
        public override void Init(int MRMethod, int ExtensionMethod)
        {
           
            int l,level;
            int i, j, k;
            double[] r=new double[CoarseImage.Depth];

            init_general(MRMethod, ExtensionMethod);

            //compute the coarse difference
            ImageWithDetail d = new ImageWithDetail();
            d.TheImage = new GImage(CoarseImage.Width, CoarseImage.Height);
            d.TheDetails = new List<GImage>();
            for(i=0;i<d.TheImage.Width;i++)
                for (j = 0; j < d.TheImage.Height; j++)
                {
                    for (k = 0; k < d.TheImage.Depth; k++)
                        r[k] = CoarseImage.data[i, j, k]- Fine_Image_Pyramid[0].TheImage.data[i, j, k];

                    d.TheImage.SetPixel(i, j,r);

                }
            
            //build the pyramid itself
            ImagePyramid = new List<GImage>();
            ImagePyramid.Add(CoarseImage);
            level = 1;
            for (l = coarse_level + 1; l <= fine_level - 1; l++)
            {
                d = MR.UpSample(d);
                double alpha = (1.0) * (l - coarse_level) / (fine_level - coarse_level);
                GImage nextimage = new GImage(d.TheImage.Width, d.TheImage.Height);
                for (i = 0; i < d.TheImage.Width; i++)
                    for (j = 0; j < d.TheImage.Height; j++)
                    {
                        for (k = 0; k < d.TheImage.Depth; k++)
                            r[k] = Fine_Image_Pyramid[level].TheImage.data[i, j, k] + (1 - alpha) * d.TheImage.data[i, j, k];

                        nextimage.SetPixel(i, j, r);

                    }
                
                ImagePyramid.Add(nextimage);
               
                level++;
            }
            ImagePyramid.Add(FineImage);

        }
    
    }
}
